﻿using BLL;
using InventoryMIS2019.conn;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InventoryMIS2019.Templates
{
    public partial class BasePurchaseBillForm : Form
    {
        List<int> selectIndexValue = new List<int>() { 0,1,2};
        int ferryValue = 0;//摆渡值：针对不同查询条件进行不同操作
        DateTime starD, endD;//记录查询时间的开始时间、结束时间
        List<string> purchaseIDList;//用于订单编号的列表集合
        string searchStr;//记录查询字符串

        BasePurchaseBillBLL bl;//业务逻辑访问类

        //以下是在子类中需要使用到的对象
        protected EmployeeBLL emBLL;
        protected PurchaseBill bo;//订单实体类
        protected PurchaseDetail pDetail;                   //详单实体类
        protected List<PurchaseDetail > detailList;//订单详情泛型集合
        protected DataGridViewTextBoxEditingControl dgvText;//DataGridView文本框控件
        public BasePurchaseBillForm()
        {
            InitializeComponent();
            bl = new BasePurchaseBillBLL();
            emBLL = new EmployeeBLL();
            bo = new PurchaseBill();
            ControlInitialState();
        }

        private void cbCondition_SelectedIndexChanged(object sender, EventArgs e)
        {
            ControlInitialState();
            if(selectIndexValue .Contains (cbCondition .SelectedIndex ))
            {
                txtSearchstring.Show();//显示查询值文本框输入框
                cbOnPress.Hide();//隐藏执行状态下拉菜单
            } 
            if (cbCondition .SelectedIndex ==3)
            {
                cbOnPress.Show();
                txtSearchstring.Hide();
            }
            if (cbCondition.SelectedIndex == 4)
            {
                cbOnPress.Hide ();
                txtSearchstring.Show ();
            }
        }

        private void ControlInitialState()
        {
            dtpPurchaseDate.Enabled = false;
            cbSupplier.Enabled = false;
            txtOnPress.Enabled = false;
            txtMemo.Enabled = false;
            lbPurchaseBill.Enabled = false;
            txtTotal.Enabled = false;
        }

        private void btnSearching_Click(object sender, EventArgs e)
        {
            Valuation();//数据效验
            if (purchaseIDList.Count != 0)
            {
                lbPurchaseBill.DataSource = purchaseIDList;
                ClearControls.ClearControl(gbSearch);
            }
            else
                MessageBox.Show("没有符合查询条件的订单！", "系统提示！");
            lbPurchaseBill.SelectedIndex = -1;
            lbPurchaseBill.Enabled = true;
        }

        private void Valuation()
        {
            List<int> tmpList = new List<int> { 0, 1, 2, 3, 4 };
            if (tmpList.Contains(cbCondition.SelectedIndex))
            {
                ferryValue = cbCondition.SelectedIndex;
                searchStr = txtSearchstring.Text.Trim();//
                if (txtSearchstring .TextLength ==0&&selectIndexValue .Contains (ferryValue ))
                {
                    MessageBox.Show("请输入需要查询条件！", "系统提示！");txtSearchstring.Focus();
                }
                starD = dtpStartDate.Value;//
                endD = dtpEndDate.Value;//
                switch (ferryValue)
                {
                    case 0:                  
                        purchaseIDList = bl.FindPurchaseBillIDByPurchaseBillID(searchStr);
                        break;
                    case 1:                    
                        purchaseIDList = bl.FindPurchaseBillIDByClerk(searchStr);
                        break;
                    case 2:                                              
                        purchaseIDList = bl.FindPurchaseBillIDByExaminer(searchStr );
                        break;
                    case 3:                     
                        purchaseIDList = bl.FindPurchaseBillIDByOnprocess(cbOnPress .SelectedIndex  );
                        break;
                    case 4:
                        purchaseIDList = bl.FindPurchaseBillIDByDate(starD, endD);
                        break;
                }
            }
            else
            {
                MessageBox.Show("请选择查询条件！", "系统提示：");
            }
        
        }

        private void lbPurchaseBill_Click(object sender, EventArgs e)
        {
            if (lbPurchaseBill .SelectedItem ==null )
            {
                MessageBox.Show("请点具体查询订单", "系统提示！");
            }
            else
            {
                string purchaseID = lbPurchaseBill.SelectedItem.ToString();//
                //
                bo = bl.FindDetailPurchaseBillByPurchaseBillID(purchaseID);

                if (bo==null )
                {
                    MessageBox.Show("未找到该订单的关联数据", "系统提示");
                }
                else
                {
                    dtpPurchaseDate.Value = bo.PurchaseDate;
                    txtPurchaseID.Text = bo.PurchaseBillID;
                    cbSupplier.SelectedValue = bo.SupplierID;
                    txtMemo.Text = bo.Memo;
                    if (bo.StockDate == null)
                    {
                        txtStockDate.Text = "待入库";
                    }
                    else
                    {
                        txtStockDate.Text = bo.StockDate.ToString();
                    }
                        txtOnPress.Text = OnPressState(bo.OnProcess);
                    
                }
                detailList = bo.PurchaseDetailList;
                if (detailList.Count == 0)
                {
                    dgvPurchaseDate.DataSource = null;
                }
                else
                    BindData();
            }
        }
        protected void BindData()
        {
            dgvPurchaseDate.DataSource = null;
            dgvPurchaseDate.DataSource = detailList;

            dgvPurchaseDate.Columns[0].DataPropertyName = "PurchaseID";
            dgvPurchaseDate.Columns[0].HeaderText = "订单编号";
            dgvPurchaseDate.Columns[0].Width = 100;
            dgvPurchaseDate.Columns[0].ReadOnly = true;

            dgvPurchaseDate.Columns[1].DataPropertyName = "PurchaseDetailID";
            dgvPurchaseDate.Columns[1].HeaderText = "详单编号";
            dgvPurchaseDate.Columns[1].Width = 100;
            dgvPurchaseDate.Columns[1].ReadOnly = true;

            dgvPurchaseDate.Columns[2].DataPropertyName = "ProductID";
            dgvPurchaseDate.Columns[2].HeaderText = "产品";
            dgvPurchaseDate.Columns[2].Width = 100;
            dgvPurchaseDate.Columns[2].ReadOnly = true;

            dgvPurchaseDate.Columns[3].DataPropertyName = "PurchasePrice";
            dgvPurchaseDate.Columns[3].HeaderText = "购入价格";
            dgvPurchaseDate.Columns[3].Width = 100;
            dgvPurchaseDate.Columns[3].ReadOnly = false;

            dgvPurchaseDate.Columns[4].DataPropertyName = "Quantity";
            dgvPurchaseDate.Columns[4].HeaderText = "数量";
            dgvPurchaseDate.Columns[4].Width = 100;
            dgvPurchaseDate.Columns[4].ReadOnly = false ;

            dgvPurchaseDate.Columns[5].DataPropertyName = "Account";
            dgvPurchaseDate.Columns[5].HeaderText = "金额";
            dgvPurchaseDate.Columns[5].Width = 100;
            dgvPurchaseDate.Columns[5].ReadOnly = false;

             for (int i=0;i <detailList .Count;i++)
            {
                var price = Convert.ToDecimal(dgvPurchaseDate.Rows[i].Cells[3].Value.ToString());
                var number = Convert.ToInt32(dgvPurchaseDate.Rows[i].Cells[4].Value.ToString());
                dgvPurchaseDate.Rows[i].Cells[5].Value = price * number;
            }
        }

        private void dgvPurchaseDate_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvPurchaseDate.CurrentRow.Selected = true;
            pDetail = detailList[e.RowIndex];
            dgvPurchaseDate.BeginEdit(true);
        }

        private void dgvPurchaseDate_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < detailList.Count; i++)
            {
                var price = Convert.ToDecimal(dgvPurchaseDate.Rows[i].Cells[3].Value.ToString());
                var number = Convert.ToInt32(dgvPurchaseDate.Rows[i].Cells[4].Value.ToString());
                dgvPurchaseDate.Rows[i].Cells[5].Value = price * number;
            }
        }

        private void dgvPurchaseDate_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 2)
                e.Value = new ProductBLL().FindByID(e.Value.ToString()).ProductName;
        }

        private string OnPressState(int value)
        {
            string pState = null;
            switch (value )
            {
                case 0:
                    pState = "执行";
                    if (bo.Custodian != null)
                        pState = "执行（已入库）";
                    else if (bo.Examiner != null)
                        pState = "执行（已审核）";
                    else
                        pState = "执行（未审核）";
                    break;
                case 1:
                    pState = "撤销";
                    break;
                case 2:
                    pState = "完成";
                    break;
            }
            return pState;


        }
    }
  }

